/**
 * 
 */
package com.pzitsoft.videoteca.dao.impl;

import java.util.List;

import net.sf.ehcache.ObjectExistsException;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.pzitsoft.videoteca.dao.DBTemplate;
import com.pzitsoft.videoteca.dao.VideoDAO;
import com.pzitsoft.videoteca.dao.hibernate.DBObject;
import com.pzitsoft.videoteca.domain.Pelicula;

/**
 * @author pferraz
 *
 */
public class VideoDAOImpl implements VideoDAO {

	private static Logger logger = Logger.getLogger(VideoDAOImpl.class);

	/* (non-Javadoc)
	 * @see com.pzitsoft.videoteca.dao.VideoDAO#getAllPeliculas()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Pelicula> getAllPeliculas() {
		Session sess = null;
		List<Pelicula> result = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query query = sess.createQuery("from p in class com.pzitsoft.videoteca.domain.Pelicula");
			result = query.list();
			if (result.isEmpty()) {
				throw new ObjectExistsException("No existen peliculas.");
			}
		} catch (HibernateException e) {
			logger.error("Error al buscar peliculas", e);
		} finally {
			if (sess != null) {
				sess.close();
			}

		}
		return result;
	}

	@Override
	public boolean addPelicula(Pelicula p) {
		// TODO Auto-generated method stub
		return DBObject.addObject(p);
	}

}
